from user.models import *
from .models import Order
from django.core.mail import send_mail, EmailMultiAlternatives
from django.conf import settings
import datetime


def send_email(email, username):
    subject = '来自青年租房管理系统的系统提醒邮件'

    text_content = '''欢迎注册青年租房管理系统！\
                    如果你看到这条消息，说明你的邮箱服务器不提供HTML链接功能，请联系管理员！'''

    html_content = '''
                    <p>用户{}您好，这里是青年租房管理系统！</p>
                    <p>这是系统提醒长租租客缴纳租金的邮件</p>
                    <p>如您已经交付过本月的租金，请忽略本邮件</p>
                    '''.format(username)

    msg = EmailMultiAlternatives(subject, text_content, settings.EMAIL_HOST_USER, [email])
    msg.attach_alternative(html_content, "text/html")
    msg.send()
    
def send_emails_all():
    for order in Order.objects.filter(status=3, rent=2):
        user = User.objects.get(id=order.uid)
        user_info = UserInfo.objects.get(id=order.uid)
        send_email(user_info.email, user.username)
        
        
def write_file():
    file = open('test.txt', 'wb+')
    file.write(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
    file.close()
        
    